//
//  CZCommentStatus.swift
//  GZWeibo666
//
//  Created by mac－gt on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZCommentStatus: NSObject {
    var loading = false
    var id:Int=0
    var user:CZUser?
    var created_at: String?
    var text:String?
    init(loading: Bool) {
        self.loading = loading
        
        super.init()
    }
    init(dict: [String: AnyObject]) {
            super.init()
            setValuesForKeysWithDictionary(dict)
        }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key=="user"{
            if let dict=value as? [String: AnyObject]
            {
                user=CZUser(dict: dict)
            }
            return
    }
        super.setValue(value, forKey: key)
    }
  
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}

    class func loadCommentId(id:Int,since_id:Int,max_id:Int,finished: (comments: [CZCommentStatus]?,total_number: Int?, previous_cursor: Int?, next_cursor: Int?, error: NSError?) -> ()){
      CZNetworkTool.sharedInstance.loadComment(id, since_id: since_id, max_id: max_id) { (result, error) -> () in
        if error != nil || result == nil{
            finished(comments: nil, total_number: nil, previous_cursor: nil, next_cursor: nil, error: error)
            return
        }
        print("\(result)")
        let total_number = result?["total_number"] as? Int

        let previous_cursor = result?["previous_cursor"] as? Int

        let next_cursor = result?["next_cursor"] as? Int
        var comments = [CZCommentStatus]()
        if let array = result!["comments"] as? [[String: AnyObject]] {
            for dict in array {
                
                let comment = CZCommentStatus(dict: dict)
                comments.append(comment)
            }
            
            finished(comments: comments, total_number: total_number, previous_cursor: previous_cursor, next_cursor: next_cursor, error: nil)
        }else{
            finished(comments: nil, total_number: total_number, previous_cursor: previous_cursor, next_cursor: next_cursor, error: nil)
        }
        }

        }
}

